package facebook;

import game.resources.Profile;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import server.DBLogic;

import com.google.code.facebookapi.Attachment;
import com.google.code.facebookapi.AttachmentMediaImage;
import com.google.code.facebookapi.AttachmentProperty;
import com.google.code.facebookapi.BundleActionLink;
import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJaxbRestClient;
import com.google.code.facebookapi.Permission;
import com.google.code.facebookapi.ProfileField;
import com.google.code.facebookapi.schema.User;
import com.google.code.facebookapi.schema.UsersGetInfoResponse;

public class FacebookUtils {

	// Return the logged in user profile
	// If it doesn't exist, create a new one from his facebook profile details
	public static Profile getFacebookProfile(FacebookJaxbRestClient client) throws FacebookException, SQLException {
        DBLogic dbl = new DBLogic();
        dbl.InitDBConnection();
		long userID = client.users_getLoggedInUser();
		Profile userProfile = dbl.queryProfile(userID);
		//if user doesn't exist:
		if (userProfile == null) {
            List<ProfileField> fields = new ArrayList<ProfileField>();
            List<Long> userIDinList= new ArrayList<Long>();
            userIDinList.add(userID);
            fields.add(ProfileField.FIRST_NAME);
            fields.add(ProfileField.LAST_NAME);
            UsersGetInfoResponse info = client.users_getInfo(userIDinList,fields);
            User user = info.getUser().get(0);
			userProfile = new Profile(userID, user.getFirstName(),user.getLastName());
			dbl.addNewProfile(userProfile);
		} 
		dbl.ShutDBConnection();
		return userProfile;
	}
	
	public static void postChallangeOnWall(FacebookJaxbRestClient client,Profile profile) throws FacebookException {
		String msg = "I am challenging you in Acquire, The ultimate online board game!";
		Attachment attachment = createAttachment(profile);
		AttachmentMediaImage media = new AttachmentMediaImage("http://delta-tomcat-vm.cs.tau.ac.il:40767/img/aqcuire-logo.jpg","http://apps.facebook.com/acquire-online/");
		attachment.setMedia(media);
		List<BundleActionLink> actionLinks = new ArrayList<BundleActionLink>();
		BundleActionLink action1 = new BundleActionLink("Fan site","http://www.facebook.com/apps/application.php?id=126564364026625&v=info");
		BundleActionLink action2 = new BundleActionLink("Play Acquire","http://apps.facebook.com/acquire-online/");
		actionLinks.add(action1);
		actionLinks.add(action2);
		client.stream_publish(msg, attachment, actionLinks, null, null);	
	}
	
    private static Attachment createAttachment(Profile profile) { 
        Attachment attachment = new Attachment(); 
        attachment.setName("Challenge in Acquire"); 
        attachment.setHref("http://apps.facebook.com/acquire-online/"); 
        attachment.setCaption(profile.getFullName() + " has a ranking of " + profile.getRating()); 
        attachment.setDescription("Would you dare answer his challenge?"); 
        return attachment; 
    }
    
	public static void updateWinnerStatus(FacebookJaxbRestClient client,Profile profile) throws FacebookException {
		if (client.users_hasAppPermission(Permission.STATUS_UPDATE)) {
			String newStatus = "I have just won in Acquire and my new rating is " + profile.getRating();
			client.users_setStatus(newStatus);
		}
	}	 
}
